package com.maoyu.cloud.user.controller;

import com.alibaba.fastjson.JSON;
import com.maoyu.cloud.constant.CloudConstant;
import com.maoyu.cloud.exception.EmailExitException;
import com.maoyu.cloud.exception.MobileExitException;
import com.maoyu.cloud.exception.UserNameExitException;
import com.maoyu.cloud.mybatis.entity.PtUserEntity;
import com.maoyu.cloud.mybatis.entity.ZtreeEntity;
import com.maoyu.cloud.mybatis.flexigrid.PadingRstType;
import com.maoyu.cloud.mybatis.flexigrid.PagingBean;
import com.maoyu.cloud.spring.controller.BaseController;
import com.maoyu.cloud.user.entity.SearchUserEntity;
import com.maoyu.cloud.user.entity.UserEntity;
import com.maoyu.cloud.user.exception.PasswordError;
import com.maoyu.cloud.user.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
    private final Logger logger = LogManager.getLogger(this.getClass());
    @Resource
    private UserService userService;
    @RequestMapping("/queryUserListByName")
    @ResponseBody
    public List<PtUserEntity> queryUserListByName(SearchUserEntity search){
        return userService.queryUserListByName(search);
    }

    @RequestMapping("/modifyPassword")
    @ResponseBody
    public String modifyPassword(@RequestParam("oldPassword") String oldPassword,@RequestParam("newPassword") String newPassword) {
        logger.info("oldPassword:" + oldPassword + "  " + "newPassword:" + newPassword);
        try {
            userService.modifyPassword(oldPassword,newPassword);
        } catch (PasswordError e) {
            return failureJson("main.old.password.error",1001);
        }
        return successJson("main.modify.password.success");
    }

    @Secured("ROLE_cldk_data_user")

    @RequestMapping("/userPage")
    public String userPage() {
        return "/user/userPage";
    }

    @RequestMapping("/getUserInfoListByPage")
    @ResponseBody
    public PadingRstType<PtUserEntity> getUserInfoListByPage(SearchUserEntity searchUserEntity,PagingBean pagingBean) {
        logger.info(pagingBean);
        PadingRstType<PtUserEntity> ptUserEntityPadingRstType = userService.getUserInfolistByPage(searchUserEntity,pagingBean);
        logger.info(ptUserEntityPadingRstType);
    return ptUserEntityPadingRstType;
    }

    @RequestMapping("/addUser")
    @ResponseBody
    public String addUser(UserEntity userEntity) {
        logger.info(userEntity);
        try {
            userService.addUser(userEntity);
        } catch (UserNameExitException e) {
            return failureJson("user.add.name.exit.failure", CloudConstant.USER_NAME_EXIT_ERROR);
        } catch (EmailExitException e) {
            return failureJson("user.add.email.exit.failure", CloudConstant.EMAIL_EXIT_ERROR);
        } catch (MobileExitException e) {
            return failureJson("user.add.mobile.exit.failure", CloudConstant.MOBILE_EXIT_ERROR);
        }
        return successJson("user.add.success");
    }

    @RequestMapping("/queryUserInfoById")
    @ResponseBody
    public UserEntity queryUserInfoById(@RequestParam("userUuid") String userUuid) {
        return userService.queryUserInfoById(userUuid);
    }

    @RequestMapping("/updateUserInfo")
    @ResponseBody
    public String updateUserInfo(UserEntity userEntity) {
        logger.info(userEntity);
        try {
            userService.updateUserInfo(userEntity);
        } catch (UserNameExitException e) {
            return failureJson("user.add.name.exit.failure", CloudConstant.USER_NAME_EXIT_ERROR);
        } catch (EmailExitException e) {
            return failureJson("user.add.email.exit.failure", CloudConstant.EMAIL_EXIT_ERROR);
        } catch (MobileExitException e) {
            return failureJson("user.add.mobile.exit.failure", CloudConstant.MOBILE_EXIT_ERROR);
        }
        return successJson("user.mod.success");
    }
    @RequestMapping("/deleteUserInfoByIds")
    @ResponseBody
    public String deleteUserInfoByIds(@RequestParam("userUuids") String userUuids) {
        userService.deleteUserInfoByIds(userUuids);
        return successJson("delete.user.success");
    }

    @RequestMapping("/queryRoleZtreeByUserId")
    @ResponseBody
    public List<ZtreeEntity> queryRoleZtreeByUserId(@RequestParam("userUuids") String userUuids) {
        List<ZtreeEntity> ztreeEntity = userService.queryRoleZtreeByUserId(userUuids);
        return ztreeEntity;
    }

    @RequestMapping("/saveRoleRelUser")
    @ResponseBody
    public String saveRoleRelUser(@RequestParam("userUuidArray") String userUuidArray, @RequestParam("roleArray") String roleArray) {
        userService.saveRoleRelUser(userUuidArray, roleArray);
        return successJson("user.allocate.role.success");
    }


    @RequestMapping("/queryRoleZtreeAll")
    @ResponseBody
    public List<ZtreeEntity> queryRoleZtreeAll() {
        List<ZtreeEntity> list = userService.queryRoleZtreeAll();
        return list;
    }

    @RequestMapping("/resetPassword")
    @ResponseBody
    public String resetPassword(@RequestParam("userUuidArray") String userUuidArray,@RequestParam("password") String password){
        logger.info("userUuidArray:"+userUuidArray);
        userService.resetPassword(userUuidArray,password);
        return successJson("user.reset.password.success");
    }
}


